package br.com.ifactory.tools;



import java.awt.Color;
import java.text.ParseException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

public class TimeChart extends ApplicationFrame {

    public TimeChart(String title) throws ParseException {
        super(title);

    }

    /**
     * Creates a chart.
     * @param dataset a dataset.
     * @return A chart.
     */
    private static JFreeChart createChart(XYDataset dataset, XYDataset dataset2, XYDataset dataset3) {
        JFreeChart chart = ChartFactory.createXYLineChart(
                "Heap Graph", // title
                "Time Since JVM was Started(seconds)", // x-axis label
                "GB", // y-axis label
                dataset, // data
                PlotOrientation.VERTICAL,
                true, // create legend?
                true, // generate tooltips?
                false // generate URLs?
                );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
        chart.setBackgroundPaint(Color.white);
//        chart.getLegend().setAnchor(Legend.SOUTH);

        // get a reference to the plot for further customisation...
        final XYPlot plot = (XYPlot) chart.getXYPlot();
        plot.setBackgroundPaint(new Color(0xEE, 0xEE, 0xFF));
        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);

        plot.setDataset(1, dataset2);
        plot.setDataset(1, dataset3);
        plot.mapDatasetToRangeAxis(1, 1);

        final NumberAxis domainAxis = new NumberAxis("GC Pause(ms)");
        plot.setRangeAxis(1, domainAxis);

        final XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
        renderer2.setShapesVisible(false);
        renderer2.setShapesFilled(false);
        plot.setRenderer(1, renderer2);
        plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
        // OPTIONAL CUSTOMISATION COMPLETED.

        // OPTIONAL CUSTOMISATION COMPLETED.
        return chart;
    }

    /**
     * Creates a dataset, consisting of two series of monthly data.
     *
     * @return the dataset.
     */
    private static XYDataset createDataset(long eventTime, int point, XYSeries s) {

        s.add(eventTime, point);
        //s1.add(eventTime, iGCPause);
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(s);
        //dataset.addSeries(s1);
        return dataset;
    }
    
    private static XYDataset createDataset2(long eventTime, int point, XYSeries s) {

        s.add(eventTime, point);
        //s1.add(eventTime, iGCPause);
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(s);
        //dataset.addSeries(s1);
        return dataset;
    
    }

    /**
     * Starting point for the demonstration application.
     *
     * @param args ignored.
     */
    public void makeChart(long eventTime, int heapUsage, int iGCPause, int commitedHeap,
                          XYSeries s0, XYSeries s1) throws ParseException {

       final XYDataset dataset = createDataset(eventTime, heapUsage, s0);
       final XYDataset dataset2 = createDataset2(eventTime, iGCPause, s1);
       final XYDataset dataset3 = createDataset2(eventTime, commitedHeap, s1);
       
       
        //FixedMillisecond fm = new FixedMillisecond(eventTime);
        //createDataset(eventTime, heapUsage, s0);
        //createDataset(eventTime, iGCPause, s1);

        JFreeChart chart = createChart(dataset, dataset2, dataset3);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        chartPanel.setMouseZoomable(true, false);
        setContentPane(chartPanel);

    }
}